package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.heima.common.dtos.AppHttpCodeEnum;
import com.heima.common.dtos.PageResponseResult;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.exception.LeadNewsException;
import com.heima.common.minio.MinIOFileStorageService;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.ThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl implements WmMaterialService {
    @Autowired
    private WmMaterialMapper wmMaterialMapper;

    @Autowired
    private MinIOFileStorageService storageService;
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {
        // 判断校验
        if (multipartFile == null){
            throw new LeadNewsException(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 从ThreadLocal取出登陆用户数据
        WmUser wmUser = (WmUser) ThreadLocalUtils.get();
        if (wmUser == null){
            throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
        }

        try {
            // 把图片上传到MinIO
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            // 获取原名
            String originalFilename = multipartFile.getOriginalFilename();
            // 获取后缀名
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".")); // .jpg

            String fileName =uuid + extName;
            String url = storageService.uploadImgFile(null,fileName, multipartFile.getInputStream());

            // 写入db
            WmMaterial wmMaterial = new WmMaterial();
            wmMaterial.setUserId(wmUser.getId());
            wmMaterial.setType((short) 0);
            wmMaterial.setIsCollection((short) 0);
            wmMaterial.setUrl(url);
            wmMaterial.setCreatedTime(new Date());

            // 写入DB
            wmMaterialMapper.save(wmMaterial);
            return ResponseResult.okResult(wmMaterial);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public PageResponseResult findList(WmMaterialDto dto) {
        System.err.println("进入listService");
        dto.checkParam();

        // 获取登录用户信息
        WmUser wmUser = (WmUser)ThreadLocalUtils.get();
        if (wmUser ==null){
            throw new LeadNewsException(AppHttpCodeEnum.NEED_LOGIN);
        }

//        // 封装条件
//        IPage<WmMaterial> iPage = new Page<>(dto.getPage(),dto.getSize());
//
//        QueryWrapper<WmMaterial> queryWrapper = new QueryWrapper<>();
//
//        // 根据用户查询
//        queryWrapper.eq("user_id",wmUser.getId());
//
//        // 收藏
//        if (dto.getIsCollection()!=null && dto.getIsCollection() ==1){
//            queryWrapper.eq("is_collection",dto.getIsCollection());
//        }
//        // 按照创建时间排序
//        queryWrapper.orderByDesc("created_time");

        // 执行查询
//        iPage = page(iPage,queryWrapper);
        // 封装结果
//        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)iPage.getTotal());
//        responseResult.setErrorMessage("查询成功");
//        responseResult.setCode(200);
//        responseResult.setData(iPage.getRecords());

        // 使用pageHelper
        PageHelper.startPage(dto.getPage(),dto.getSize());
        List<WmMaterial> list = wmMaterialMapper.findById(wmUser.getId(),dto.getIsCollection());

        PageInfo<WmMaterial> pageInfo = new PageInfo<>(list);

        // 封装结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int)pageInfo.getTotal());
        responseResult.setErrorMessage("查询成功");
        responseResult.setCode(200);
        responseResult.setData(pageInfo.getList());
        return responseResult;

    }
}
